<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'

  require('echarts/theme/macarons') // echarts theme
  import {debounce} from '@/utils'

  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '350px'
      }
    },
    data() {
      return {
        chart: null,
        parkList: [],
        dataName: []
      }
    },
    mounted() {
      this.initChart();
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      window.removeEventListener('resize', this.__resizeHanlder)
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      forList(parkList) {
        for (var i = 0; i < parkList.length; i++) {
          this.dataName.push(parkList[i].parkname)
        }
      },
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons');
        var dataStyle = {
          normal: {
            shadowBlur: 40,
            shadowColor: 'rgba(40, 40, 40, 0.5)',
          }
        };
        this.chart.setOption({

          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          calculable: true,
          series: [
            {
              name: '设备故障占比',
              type: 'pie',
              roseType: 'radius',
              radius: [15, 100],
              center: ['50%', '50%'],
              itemStyle: dataStyle,
              data: [
                {value: 5, name: '频闪'},
                {value: 3, name: '显示信息有误'},
                {value: 38, name: '断电'},
                {value: 29, name: '屏幕损坏'},
                {value: 20, name: '通讯中断'},
                {value: 11, name: '其它'}
              ],
              animationEasing: 'cubicInOut',
              animationDuration: 2600
            }
          ]
        })
      }
    }
  }
</script>
